/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkeddata.arida.ufc.br.servlet;

import java.io.IOException;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import linkeddata.arida.ufc.br.VCD.project.CreateVCDProject;

/**
 *
 * @author jeovane
 */
public class CreatePlainExecuteServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        String select = request.getParameter("select");
        String[] parameterListCondition = request.getParameterValues("parameterListCondition[]");
        int limit = Integer.parseInt(request.getParameter("limit"));
        String nameDestination = request.getParameter("nameDestination");
        String[] destinationParameter = request.getParameterValues("destinationParameter[]");
        String[] query = request.getParameterValues("query[]");
        String writerXML = "";

        StringTokenizer st = new StringTokenizer(select, ", ");
        String[] parameterListSelect = new String[st.countTokens()];
        int i = 0;
        while (st.hasMoreTokens()) {
            parameterListSelect[i] = st.nextToken();
            i++;
        }

        CreateVCDProject project = new CreateVCDProject(name, type, parameterListSelect, parameterListCondition, limit, nameDestination, destinationParameter, query);

        try {
            writerXML = project.createVcdXML();
        } catch (Exception ex) {
            Logger.getLogger("ERROR CREATE PLAIN: " + CreatePlainExecuteServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(writerXML);
    }
}
